package com.starblink.store.memanage.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.common.view.store.SearchStoreRecommendModel
import com.starblink.rocketreserver.FetchMySubscribeMerchantWebQuery
import com.starblink.rocketreserver.FetchSubscribeMerchantWebRecommendQuery
import com.starblink.rocketreserver.ReadSubscribeMerchantWebMutation
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.rocketreserver.type.MerchantWebRecommendReq
import kotlinx.coroutines.launch

class ManageStoreVM : BaseViewModel() {
    var subscribePageNo = 1
    var recoPageNo = 1
    val pageSize = 20

    var list: MutableLiveData<MutableList<Any>> = MutableLiveData(mutableListOf())

    var loadedRecommend: Boolean = false //是否已加载

    //我的-顶部订阅店铺数据
    fun fetchMySubscribeMerchantWeb(
        showLoading: Boolean = false,
        initRefresh: Boolean = false
    ) {
        if (initRefresh) {
            subscribePageNo = 1
        }
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchMySubscribeMerchantWebQuery(
                    subscribePageNo,
                    pageSize,
                ), showDefaultLoading = showLoading
            )
            response?.data?.fetchMySubscribeMerchantWeb?.subList?.map { it.merchantWebVOF }
                ?.let { data ->
                    if (initRefresh) {
                        list.value!!.clear()
                    }
                    if (data.isNotEmpty()) {
                        list.value!!.addAll(data)

                        val hasNext = response.data!!.fetchMySubscribeMerchantWeb.hasNext
                        if (!hasNext) {
                            loadRecommendStores()
                        }
                        subscribePageNo++

                        list.value = list.value
                    }

                    if (list.value!!.isEmpty()) {//订阅数据为空时展示空布局
                        addEmptyView()
                    }
                }
        }
    }

    private var recSizeTag = 0
    fun loadRecommendStores() {
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchSubscribeMerchantWebRecommendQuery(
                    merchantParams = MerchantWebRecommendReq(
                        accountId = Optional.present(null),
                        countryCode = Optional.present(null),
                        5,
                        System.currentTimeMillis().toString(),
                        recoPageNo,
                        pageSize
                    )
                )
            )
            var data: List<SearchStoreRecommendModel> = listOf()
            response?.data?.fetchSubscribeMerchantWebRecommend?.merchantWebList?.let { it ->
                data = it?.mapIndexed { index, it ->
                    SearchStoreRecommendModel(
                        it.merchantWebVOF,
                        index = index + recSizeTag,
                    )
                } ?: arrayListOf()
                recSizeTag += data.size
                if (data.isNotEmpty()) {
                    if (recoPageNo == 1) {
                        list.value?.add("Recommended Stores")
                    }
                    loadedRecommend = true
                    recoPageNo++
                }
                list.value!!.addAll(data)
                list.value = list.value
            }
//            if (recoPageNo == 1 && list.isEmpty()) {
//                loadNoData()
//            }
        }
    }

    fun addEmptyView() {
        list.value?.add(0)
        list.value = list.value
        loadRecommendStores()
    }

    //其它地方订阅店铺 同步增加
    fun addSubscribeStore(merchant: MerchantWebVOF) {
        if (list.value?.isNotEmpty() == true && list.value?.first() is Int) {
            list.value?.removeAt(0)
        }
        list.value?.add(0, merchant)
        list.value = list.value
    }

    fun removeItem(position: Int) {
        list.value?.removeAt(position)
        list.value = list.value
    }


    fun readMessage(pos: Int, brand: MerchantWebVOF) {
        viewModelScope.launch {
            val response = SkGqlClient.skMutation(
                ReadSubscribeMerchantWebMutation(brand.id!!)
            )
//            if (response?.data?.readSubscribeBrand == true) {
//                list.value?.set(pos, brand)
//                list.value = list.value
//            }
        }
    }
}